# -*- coding: utf-8 -*-
import pandas as pd  # 用于生成满足绘图要求的数据格式
import numpy as np  # 用于展示横坐标
from matplotlib import pyplot as plt  # 用于绘制折线图
city = []
data1 = []
def test():
    global data1
    # global data2
    global city
    import pandas as pd
    from sqlalchemy import create_engine
    engine = create_engine('mysql+pymysql://root:123456@localhost:3306/pythondata')
    # 查询语句，选出employee表中的所有数据
    sql = ''' select  city from test  ;'''
    # read_sql_query的两个参数: sql语句， 数据库连接
    city = pd.read_sql_query(sql, engine)
    city = np.array(city)  # 先将数据框转换为数组
    city = city.tolist()  # 其次转换为列表

    sql = ''' select  price from test  ;'''
    # read_sql_query的两个参数: sql语句， 数据库连接
    data1 = pd.read_sql_query(sql, engine)
    data1 = np.array(data1)  # 先将数据框转换为数组
    data1 = data1.tolist()  # 其次转换为列表

def plot():
    # ********* Begin *********#
    global data1
    # global data2
    global city
    fig, ax = plt.subplots()  # 创建一个图像和一个坐标轴
    # ax.plot(population["Year"],popultaion["Population"]) #绘制折线图
    ax.set_xlabel("Year", fontsize=12)  # 设置x轴标签
    ax.set_ylabel("Population", fontsize=12)  # 设置y轴标签
    # plt.show() #展示图像
    my_x_ticks = np.arange(1960, 2011, 5)
    plt.xticks(my_x_ticks)
    plt.grid(visible=True, color='r', linestyle='--', linewidth=1, alpha=0.3, axis='x', which="major")
    ax.plot(city, data1, linewidth=1, c='#00CC88', marker='*', markersize=4,label='Y1')
    plt.show()
    # ********* End *********#
    plt.savefig('/world-population.jpg')  # 保存为png格式
    plt.close()  # 关闭画布窗口

test()
plot()